package com.keya.selector;

import com.baomidou.mybatisplus.annotation.TableField;
import com.keya.dicomtask.DicomTask;
import com.keya.info.QcInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Case;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

@Component
@Slf4j
public class ConditionRegistry {


    public static Map<Class, Map<String, String>> CONDITION_MAP = new HashMap<>();

    public static Class[] registryClass = new Class[]{
            Case.class, DicomTask.class, QcInfo.class
    };

    @PostConstruct
    public void registry() {
        for (Class clazz : registryClass) {
            reg(clazz);
        }
        log.info("condition加载完成");
    }

    public void reg(Class clazz) {
        Map<String, String> map = CONDITION_MAP.computeIfAbsent(clazz, k -> new HashMap<>());
        Field[] declaredFields = clazz.getDeclaredFields();
        for (Field field : declaredFields) {
            field.setAccessible(true);
            String name = field.getName();
            if (field.isAnnotationPresent(TableField.class)) {
                TableField annotation = field.getAnnotation(TableField.class);
                String value = annotation.value();
                map.put(name, value);
            } else {
                map.put(name, name.replaceAll("([A-Z])", "_$1").toLowerCase());
            }
        }
    }

}
